define(function(require, exports){
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');
	var $		= require('jquery');

	var MoreRule = view.container.extend({
		init: function(config, parent){
			config = pubjs.conf(config, {
				'class': 'P-info'
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			var self	= this;
			var el		= self.getContainer();

			el.append($([
				'<div class="moreRuleModule mt10">',
					'<div pub-mod="@base/common/drop.drop" pub-name="dim" class="mb10"></div>',
					'<div pub-name="grid" pub-mod="addAccount/grid/dim.base" ></div>',
				'</div>'
			].join('')));

			var config = {
				'dim': {
					'def': LANG('收费维度'),
					'showResult': false,
					'options': [
						{'name':LANG('渠道'), 'id': 'Channel'},
						//{Name:LANG('域名'), _id: 'Domain'}, // 暂时隐藏
						{'name':LANG('广告位'), 'id': 'Spot'}
					],
					'width': 120,
					'panel_config': {
						'hasSearch': false
					}
				},
				'grid': {
					"operation":{
						render: 'renderOperation',
						cls:'M-gridOPCursor',
						width:120
					},
					'dpOptions': [
						{'_id': 1, 'Name': LANG('溢价')}
						// ,{ '_id': 2, 'Name':LANG('CPM')}
					]
				}
			};

			this.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){
			var el = this.getContainer();
			this.$chargeAmount	= el.find('.chargeAmount');
			this.$chargeUnit	= el.find('.chargeUnit');
		},
		onDropChange: function(ev){
			this.showPopwin(ev.param.id);
			return false;
		},
		// 下拉框选项选中事件
		onOptionChange: function(ev){
			if(ev.name == 'dim'){
				//var type = ev.param.option._id;
			}else if(ev.name == 'defaultDropdown'){
				var unit_el = this.$chargeUnit;
				var id = ev.param.id;
				switch (id){
					case '1':
						unit_el.text('%');
						break;
					default:
						unit_el.text(LANG('元'));
				}
			}
			return false;
		},
		// 显示弹框
		showPopwin: function(type, index, ids){
			var self = this;
			var name = type.toLowerCase() + 'Win';
			var dlg = self.get(name);
			if (!dlg){
				self.createAsync(name, 'addAccount/popwin/moreRule.' + name, function(mod){
					mod.show();
					if(ids){
						mod.setData(index, ids);
					}
				});
			}
			else {
				dlg.reset();
				if(ids){
					dlg.setData(index, ids);
				}
				dlg.show();
			}
		},
		// 弹框保存事件
		onPopwinSave: function(ev){
			var item = null;
			var data = ev.param;

			if(!data.Ids.length){
				if(data.hasOwnProperty('Index'))
				{
					this.$.grid.removeRow(data.Index);
				}
				return false;
			}

			// 转换数据结构
			switch(ev.name){
				case 'channelWin':		// 渠道
					var tmpId = [];
					data.Ids.forEach(function(val){
						tmpId.push(val.id);
					})
					item = {
						'Cat': 'Channel',
						'Ids': tmpId
					};
					break;
				case 'spotWin':	// 广告位
					item = {
						'Cat': 'Spot',
						'Ids': data.Ids
					};
					break;
			}

			// 如果Index不带值是新增，带值为更新
			if(!util.isNumber(data.Index)){
				this.$.grid.addRow(item);
			}else{
				this.$.grid.updateRow(data.Index, item);
			}
			return false;
		},
		// 渲染操作列
		renderOperation: function(i, val, data, con){
			var html = [
				'<a data-op="edit" data-id="'+data.id+'" data-type="'+data.category+'">'+LANG("编辑")+'</a>',
				'<a data-op="remove" data-id="'+data.id+'">'+LANG("删除")+'</a>'
			].join(' | ')
			return html;
		},
		// 操作列选中事件
		onListOpClick: function(ev, elm){
			var index	= ev.param.index;
			var op		= ev.param.op;
			var data	= ev.param.data;
			var ids;
			switch (op){
				case 'edit':	// 编辑
					ids = data.Ids;
					this.showPopwin(data.Cat, index, ids);
					break;
				case 'remove':	// 删除
					this.$.grid.removeRow(index);
					break;
			}
			return false;
		},
		setData: function(data){
			if(data.Premium && data.Premium.data.length){
				this.$data = data.Premium;
			}
			this.$.grid.setData(data.Premium.data || []);
		},
		getData: function(){
			return this.$.grid.getData();
		},
		reset: function(){
			this.$data = [];
			this.$.grid.setData([]);
			this.$chargeAmount.val('');
			return this;
		},
		// 检测重复
		checkIsRepeat: function(data){
			var isRepeat = false;
			if(data){
				util.each(data, function(item){
					if(item && item.Ids)
					{
						var hash = {};
						util.each(item.Ids, function(sub){
							var tmp = sub.toString();
							if(hash[tmp])
							{
								isRepeat = true;
							}
							hash[tmp] = true;
						})
					}
				});
			}
			return isRepeat;
		},
		checkValue: function(data)
		{
			var status = false;
			if(data){
				util.each(data, function(item){
					if(item && item.Value <= 0)
					{
						status = true;
					}
				});
			}
			return status;
		}
	});

	exports.base = MoreRule;



});